/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mylibrary;

/**
 *
 * @author nafeez
 */
public abstract class AdvancedThread extends Thread {

    private boolean runningEnabled;
    private boolean stopped;
    private Handler handler;

    public AdvancedThread(Handler handler) {
        super();
        this.handler = handler;
        stopped = false;
        runningEnabled = true;
    }

    public AdvancedThread(String name, Handler handler) {
        super(name);
        this.handler = handler;
        stopped = false;
        runningEnabled = true;
    }

    @Override
    public void run() {
        while (runningEnabled) {
//            Main.prl("thread loop running");
            threadLoop();
        }
        stopped = true;
    }

    public void sendMessage(Message m) throws Exception {
        handler.sendMessage(m);
    }

    public abstract void threadLoop();

    public void cancel() {
        runningEnabled = false;
    }

    public boolean isStopped() {
        return stopped;
    }
}
